package io.gitee.zhangbinhub.admin.oauth.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import com.mybatisflex.annotation.Column
import com.mybatisflex.annotation.Id
import com.mybatisflex.annotation.RelationManyToMany
import com.mybatisflex.annotation.Table
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import org.apache.commons.lang3.builder.EqualsBuilder
import org.apache.commons.lang3.builder.HashCodeBuilder
import org.noear.snack4.annotation.ONodeAttr

@Table("t_user")
@ApiModel("用户信息")
data class User(
    @Id
    @ApiModelProperty(name = "用户ID")
    var id: String = "",
    @ApiModelProperty(name = "用户名称")
    var name: String = "",
    @ApiModelProperty(name = "登录号")
    var loginNo: String = "",
    @JsonIgnore
    @field:ONodeAttr(ignore = true)
    var password: String = "",
    @ApiModelProperty(name = "手机号")
    var mobile: String? = null,
    @ApiModelProperty(name = "用户级别")
    var levels: Int = 0,
    @ApiModelProperty(name = "是否启用")
    var enabled: Boolean = false,
    @ApiModelProperty(name = "头像")
    var avatar: String? = null,
    @ApiModelProperty(name = "序号")
    var sort: Int = 0,
    @ApiModelProperty(name = "所属机构")
    @RelationManyToMany(
        joinTable = "t_user_organization_set", // 中间表
        joinSelfColumn = "user_id",
        joinTargetColumn = "organization_id"
    )
    var organizationSet: MutableList<Organization> = mutableListOf(),
    @ApiModelProperty(name = "可管理的机构")
    @RelationManyToMany(
        joinTable = "t_user_organization_mng_set", // 中间表
        joinSelfColumn = "user_id",
        joinTargetColumn = "organization_id"
    )
    var organizationMngSet: MutableList<Organization> = mutableListOf(),
    @ApiModelProperty(name = "所属角色")
    @RelationManyToMany(
        joinTable = "t_user_role_set", // 中间表
        joinSelfColumn = "user_id",
        joinTargetColumn = "role_id"
    )
    var roleSet: MutableList<Role> = mutableListOf(),
    @ApiModelProperty(name = "最后修改密码时间")
    var lastUpdatePasswordTime: Long? = null,
    @ApiModelProperty(name = "密码是否过期")
    @Column(ignore = true)
    var passwordExpire: Boolean? = null
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || javaClass != other.javaClass) return false
        val user = other as User
        return EqualsBuilder()
            .appendSuper(super.equals(other))
            .append(id, user.id)
            .append(name, user.name)
            .append(loginNo, user.loginNo)
            .append(mobile, user.mobile)
            .append(levels, user.levels)
            .append(enabled, user.enabled)
            .append(sort, user.sort)
            .append(lastUpdatePasswordTime, user.lastUpdatePasswordTime)
            .isEquals
    }

    override fun hashCode(): Int =
        HashCodeBuilder(17, 37)
            .appendSuper(super.hashCode())
            .append(id)
            .append(name)
            .append(loginNo)
            .append(mobile)
            .append(levels)
            .append(enabled)
            .append(sort)
            .append(lastUpdatePasswordTime)
            .toHashCode()

    override fun toString(): String =
        StringBuilder("User(")
            .append("id=$id")
            .append(",name=$name")
            .append(",loginNo=$loginNo")
            .append(",mobile=$mobile")
            .append(",levels=$levels")
            .append(",enabled=$enabled")
            .append(",sort=$sort")
            .append(",lastUpdatePasswordTime=$lastUpdatePasswordTime")
            .append(")")
            .toString()
}
